<template>
  <div class="ad-create-or-update">
    <el-card>
      <div slot="header" class="clearfix">
        <span>创建广告</span>
      </div>
      <el-form :model="adInfo" :rules="rules" ref="adInfo" label-width="100px" class="demo-adInfo">
        <el-form-item label="广告名称" prop="name">
          <el-input v-model="adInfo.name"></el-input>
        </el-form-item>
        <el-form-item label="广告位置">
          <el-select v-model="adInfo.spaceId" placeholder="请选择">
            <el-option
              v-for="item in spaceInfo"
              :key="item.id"
              :label="item.name"
              :value="item.id">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="开始时间">
          <el-date-picker
            v-model="adInfo.createTime"
            type="datetime"
            placeholder="选择日期时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="到期时间">
          <el-date-picker
            v-model="adInfo.updateTime"
            type="datetime"
            placeholder="选择日期时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="上线/下线">
          <el-radio v-model="adInfo.status" label="1">上线</el-radio>
          <el-radio v-model="adInfo.status" label="0">下线</el-radio>
        </el-form-item>
        <el-form-item label="广告图片">
          <el-input v-model="adInfo.img"></el-input>
        </el-form-item>
        <el-form-item label="广告链接">
          <el-input v-model="adInfo.link"></el-input>
        </el-form-item>
        <el-form-item label="广告备注">
          <el-input type="textarea" v-model="adInfo.text"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="submitForm('adInfo')">提交</el-button>
          <el-button @click="resetForm('adInfo')">重置</el-button>
        </el-form-item>
      </el-form>
    </el-card>
  </div>
</template>

<script>
import Vue from 'vue'
import { getAllInfo, saveOrUpdate, getAdById } from '@/services/advertise'
export default Vue.extend({
  name: 'CreateAdvertOrUpdate',
  props: {
    id: {
      type: String
    }
  },
  data () {
    return {
      adInfo: {
        name: '',
        spaceId: '',
        createTime: '',
        updateTime: '',
        status: '1',
        link: '',
        text: '',
        sort: 0
      },
      spaceInfo: [],
      rules: {
        name: [
          { required: true, message: '请输入活动名称', trigger: 'blur' },
          { min: 3, max: 5, message: '长度在 3 到 5 个字符', trigger: 'blur' }
        ]
      }
    }
  },
  created () {
    this.loadSpaceInfo()
    if (this.id) {
      this.loadAdInfo()
    }
  },
  methods: {
    async loadAdInfo () {
      const { data } = await getAdById({
        id: this.id
      })
      this.adInfo = data.content
    },
    async loadSpaceInfo () {
      const { data } = await getAllInfo()
      console.log(data)
      this.spaceInfo = data.content
    },
    async submitForm (formName) {
      // 校验
      this.$refs[formName].validate(async (valid) => {
        if (valid) {
          // 发请求
          await saveOrUpdate(this.adInfo)
          this.$message.success('操作成功')
          this.$router.back()
        } else {
          console.log('error submit!!')
          return false
        }
      })
    },
    resetForm (formName) {
      this.$refs[formName].resetFields()
    }
  }
})
</script>

<style>

</style>
